package org.example.jwtdemo.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.jwtdemo.entity.Category;
import org.example.jwtdemo.service.CategoryService;
import org.example.jwtdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //今天的主题

    //创建分类
    @Transactional//事务注解
    @PostMapping("/add")
    public Result createCategory(@RequestBody Category category, @RequestHeader("Authorization") String token) {
        log.info("test token: {}", token);
        categoryService.add(category);
        return Result.success();
    }


    //删除分类
    @PostMapping("/delete")
    public Result deleteCategory(@RequestBody List<Integer> ids, @RequestHeader("Authorization") String token) {
        log.info("test token: {}", token);
        categoryService.delete(ids);

        return Result.success("删除" + ids + "成功");
    }

    @PostMapping("/delete/{id}")
    public Result deleteCategoryById(@PathVariable Integer id) {
        categoryService.deleteById(id);
        return Result.success("删除" + id + "成功");
    }
    //查询分类列表
//    @GetMapping("/list")
//    public Result listCategory(@RequestHeader("Authorization") String token) {
//        log.info("test token: {}", token);
//        List<Category> categoryList = categoryService.list();
//        return Result.success(categoryList);
//    }

    //查询分类列表
    @GetMapping("/list")
    public Result listCategory() {
        List<Category> categoryList = categoryService.list();
        return Result.success(categoryList);
    }

    //分页查询 页码 每页条数
    @GetMapping("/page")
    public Result selectPage(Category category, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Category> page = categoryService.selectPage(category, pageNum, pageSize);
        return Result.success(page);
    }
}
